/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com */

/*
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "material.h"

namespace tri {

std::map<std::string,TMaterial*> TMaterial::mMaterialMap = std::map<std::string,TMaterial*>();

TMaterial::TMaterial():mFilter(false),mMipmap(false){
	setSpecular(tri::graphics::RGBA(0,255,255,255));
	setColor(255,255,255,255);
	setShininess(50.0);
}

TMaterial::~TMaterial(){
	std::string image = getImage();
	tri::deleteTexture(image);
}

void TMaterial::setColor(int r,int g, int b,int a){
	mColor.setValue(r,g,b,a);
}

void TMaterial::setColor(tri::graphics::RGBA rgba){
	mColor.setValue(rgba);
}

void TMaterial::setTargetColor(tri::graphics::RGBA rgba,float t){
	mColor.setTargetValue(rgba.red,rgba.green,rgba.blue,rgba.alpha,t);
}

tri::graphics::RGBA TMaterial::getColorAsRGBA(){
	return mColor.asRGBA();
}


tri::graphics::TColor TMaterial::getColor(){
	return mColor;
}

void TMaterial::setSpecular(tri::graphics::RGBA rgba){
	mSpecular[0] = (float)rgba.red/255;
	mSpecular[1] = (float)rgba.green/255;
	mSpecular[2] = (float)rgba.blue/255;
	mSpecular[3] = (float)rgba.alpha/255;
}

void TMaterial::setShininess(float s){
	mShininess[0] = s;
}

void TMaterial::apply(){
	applyTexture(getImage());
	mColor.apply();
	glMaterialfv(GL_FRONT, GL_SPECULAR, mSpecular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mShininess);
}

void TMaterial::setImage(std::string image,bool filter,bool mipmap) {
	mImage = image;
	mMipmap = mipmap;
	mFilter = filter;
}

std::string TMaterial::getImage() {
	return mImage;
}

bool TMaterial::isImageFiltered(){
	return mFilter;
}

bool TMaterial::isImageMipmap(){
	return mMipmap;
}

tri::TTexture* TMaterial::getTexture(){
	if (getImage().compare("")!=0) {
		TTexture* tex = tri::getTexture(getImage());
		if (tex!=NULL) {
			if (!tex->created()) {
				//std::cout<<"Loading GLyph Texture "<<_image<< " mipmap :"<<mMipmap<<" filter :"<<mFilter<<std::endl;
				tex->load(getImage(),isImageMipmap(),isImageFiltered());
			}
			return tex;
		} else {
			tri::createTexture(getImage());
			TTexture* tex = tri::getTexture(getImage());
			if (tex!=NULL) {
			    //std::cout<<"Loading GLyph Texture "<<_image<< " mipmap :"<<mMipmap<<" filter :"<<mFilter<<std::endl;
				tex->load(getImage(),isImageMipmap(),isImageFiltered());
				return tex;
			}else{
				unbindAllTextures();
			}
		}
	} else {
		unbindAllTextures();
	}
	return NULL;
}

void TMaterial::createMaterial(std::string key){
	if(!TMaterial::materialExists(key)){
		TMaterial::mMaterialMap[key] = new TMaterial();
	}
}

bool TMaterial::materialExists(std::string key){
	std::map<std::string, TMaterial* >::iterator it;
	for ( it=TMaterial::mMaterialMap.begin() ; it != TMaterial::mMaterialMap.end(); it++ ){
		if((*it).first==key){
			return true;	
		}
	}
	return false;
}

void TMaterial::deleteMaterial(std::string key){
	if(TMaterial::materialExists(key)){
		delete TMaterial::mMaterialMap[key];
		TMaterial::mMaterialMap[key] = NULL;
		TMaterial::mMaterialMap.erase(key);
	}
}

TMaterial* TMaterial::getMaterial(std::string key){
	if(TMaterial::materialExists(key)){
		return TMaterial::mMaterialMap[key];
	}
	return NULL;
}

}

